//
//  newsListViewController.m
//  项目一
//
//  Created by nick on 15/7/22.
//  Copyright (c) 2015年 nick. All rights reserved.
//

#import "newsListViewController.h"
#define sheight [UIScreen mainScreen].bounds.size.height
#define swidth [UIScreen mainScreen].bounds.size.width
#define sNavHeight 64
#define sTabBarHeight 49
#import "DataService.h"
#import "newsModal.h"
#import "newsListCell.h"
#import "baseNavigationController.h"
#import "photoViewController.h"
@interface newsListViewController (){
    NSMutableArray *modalArray;
    NSMutableArray *imageArray;
}

@end

@implementation newsListViewController
#pragma mark - tools
- (void)_dataLoad{
    modalArray = [[NSMutableArray alloc]init];
    imageArray = [[NSMutableArray alloc]init];
    NSArray *dataArray = [DataService setDataWithJason:@"image_list"];
    for (NSDictionary *dic in dataArray) {
        newsModal *modal = [[newsModal alloc]init];
        [modal setValuesForKeysWithDictionary:dic];
        [modalArray addObject:modal];
        [imageArray addObject:modal.image];
    }
}
#pragma mark - createSubView
- (void)_setNavigationBar{
    self.navigationItem.title = @"图片新闻";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
}
- (void)_createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((swidth-50)/4, (swidth-50)/4);
    UICollectionView *cvc = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, swidth, sheight-64-49) collectionViewLayout:layout];
    cvc.delegate = self;
    cvc.dataSource = self;
    [self.view addSubview:cvc];
    
    [cvc registerClass:[newsListCell class] forCellWithReuseIdentifier:@"cell"];
}

#pragma mark - datasourceAndDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return modalArray.count;
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    newsListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    //cell.backgroundColor = [UIColor whiteColor];
    cell.modal = modalArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    photoViewController *vc = [[photoViewController alloc]init];
    baseNavigationController *nvc = [[baseNavigationController alloc]initWithRootViewController:vc];
    nvc.navigationBar.translucent = YES;
    vc.index = indexPath.row;
    vc.imageUrlArray = (NSArray*)imageArray;
    [self presentViewController:nvc animated:YES completion:nil];
}
#pragma mark - didload
- (void)viewDidLoad {
    [super viewDidLoad];
    [self _dataLoad];
    [self _setNavigationBar];
    [self _createCollectionView];

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
